package zhang.automobileservice.activity.firstpagedetial;

import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.Response;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import adapter.PopCarBrandAdapter;
import adapter.PopCarPriceAdapter;
import adapter.SecondCarAdapter;
import bean.PopCarBrandBean;
import bean.Second_Car;
import utils.MyUrl;
import utils.RecyclerItemClickListener;
import utils.RequestQueueUtils;
import zhang.automobileservice.FarterActivity;
import zhang.automobileservice.R;
import zhang.automobileservice.SecondCarDetialActivity;

public class SecondCarActivity extends FarterActivity implements View.OnClickListener {

    private ImageButton finish;
    private TextView tv_01;
    private TextView tv_02;
    private TextView tv_03;
    private PullToRefreshListView lv_secondcar;
    private View view, view1, view2;
    private int pos1, pos2, pos3;
    private Map<String, String> map = new HashMap<>();
    private ArrayList<Second_Car> addList;
    private String price = "";
    private String Km = "";
    private String carAge = "";
    private String Type = "";
    private GridView gridView01;
    private TextView tv_defaut;
    private ImageView iv_defaut;
    private TextView tv_priceMin;
    private ImageView iv_priceMin;
    private TextView tv_priceMax;
    private ImageView iv_priceMax;
    private TextView tv_carAge;
    private ImageView iv_carAge;
    private TextView tv_kilo;
    private ImageView iv_kilo;
    private RelativeLayout rl_defaut;
    private RelativeLayout rl_priceMin;
    private RelativeLayout rl_priceMax;
    private RelativeLayout rl_carAge;
    private RelativeLayout rl_kilo;
    private String order;
    private PopupWindow popWindow;
    private RecyclerView rv_list;
    private ArrayList<PopCarBrandBean> arrayList = new ArrayList<>();
    private StaggeredGridLayoutManager manger;
    private SecondCarAdapter adapter;
    private int count = 1;
    private String url;
    private ListView lv_carBrand;
    private ArrayList<String> list;
    private PopupWindow popWindow01;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second_car);
        initView();
        addList = new ArrayList<>();
        adapter = new SecondCarAdapter(SecondCarActivity.this, addList,null);
        lv_secondcar.setAdapter(adapter);
        initData();
    }

    private void initData() {
        url = MyUrl.Font_Url + MyUrl.queryPagedata +"?s_price=" + price + "&km=" + Km + "&s_date=" + carAge + "&order=" + order+"&page=" + count;
        if(count==1){
            addList.clear();
        }
            RequestQueueUtils.exectueStringRequest_post(this, url, new Response.Listener<String>() {
                @Override
                public void onResponse(String s) {
                    JSONObject o ;
                    try {
                        o = new JSONObject(s);
                        if ("0".equals(o.getString("code"))) {
                            JSONArray obj = o.getJSONArray("data");
                            for (int i = 0; i < obj.length(); i++) {
                                JSONObject jsonObject = obj.getJSONObject(i);
                                Second_Car secondCar = new Second_Car();
                                secondCar.setImage(jsonObject.getString("image"));
                                secondCar.setKm(jsonObject.getInt("km") + "");
                                secondCar.setPrice(jsonObject.getDouble("price") + "");
                                secondCar.setBuyDate(jsonObject.getString("buyDate"));
                                secondCar.setTab(jsonObject.getString("tab"));
                                secondCar.setTitle(jsonObject.getString("title"));
                                secondCar.setId(jsonObject.getInt("id") + "");
                                addList.add(secondCar);
                            }
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    adapter.notifyDataSetChanged();
                    lv_secondcar.onRefreshComplete();
                    lv_secondcar.setMode(PullToRefreshBase.Mode.BOTH);
                }
            }, map);
        lv_secondcar.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                count=1;
                initData();
            }
            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                count++;
                initData();
            }
        });
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.finish:
                finish();
                break;
            case R.id.tv_01:
                tv_01.setTextColor(getResources().getColor(R.color.discover));
                tv_02.setTextColor(getResources().getColor(R.color.secondcartext));
                tv_03.setTextColor(getResources().getColor(R.color.secondcartext));
                popWindow = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
                popWindow.setBackgroundDrawable(new ColorDrawable(0x000000));
                popWindow.showAsDropDown(tv_01);
                break;
            case R.id.tv_02:
                tv_02.setTextColor(getResources().getColor(R.color.discover));
                tv_01.setTextColor(getResources().getColor(R.color.secondcartext));
                tv_03.setTextColor(getResources().getColor(R.color.secondcartext));
                popWindow01 = new PopupWindow(view1, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
                popWindow01.setBackgroundDrawable(new ColorDrawable(0x000000));
                popWindow01.showAsDropDown(tv_02);
                requestData();
                rv_list.addOnItemTouchListener(new RecyclerItemClickListener(this, new RecyclerItemClickListener.OnItemClickListener() {
                    @Override
                    public void onItemClick(View view, int position) {
                        TextView tv_carName = (TextView) view.findViewById(R.id.tv_carName);
                        Type = tv_carName.getText().toString().trim();
                        map.put("type",Type);
                        addList.clear();
                        count=1;
                        initData();
                        popWindow01.dismiss();
                    }
                }));
                lv_carBrand.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                      TextView tv_carContent = (TextView) view.findViewById(R.id.tv_carContent);
                        Type = tv_carContent.getText().toString().trim();
                        map.put("type",Type);
                        count=1;
                        initData();
                        popWindow01.dismiss();
                    }
                });
                break;
            case R.id.tv_03:
                tv_01.setTextColor(getResources().getColor(R.color.secondcartext));
                tv_02.setTextColor(getResources().getColor(R.color.secondcartext));
                tv_03.setTextColor(getResources().getColor(R.color.discover));
                gridView01 = (GridView) view2.findViewById(R.id.gv_price01);
                gridView01.setAdapter(new PopCarPriceAdapter(getApplicationContext(), getStrList01(), pos1));
                gridView01.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        TextView textView = (TextView) parent.getChildAt(position).findViewById(R.id.bt_content);
                        TextView textView1 = (TextView) parent.getChildAt(pos1).findViewById(R.id.bt_content);
                        textView1.setBackground(getDrawable(R.drawable.carprice_background));
                        textView1.setTextColor(getResources().getColor(R.color.carprice));
                        textView.setBackground(getDrawable(R.drawable.loginedittext_background));
                        textView.setTextColor(getResources().getColor(R.color.textcolor));
                        if (position == 0) {
                            price = "";
                        } else {
                            price = textView.getText().toString().split("万")[0];
                        }
                        count=1;
                        initData();
                        pos1 = position;
                    }
                });
                GridView gridView02 = (GridView) view2.findViewById(R.id.gv_price02);
                gridView02.setAdapter(new PopCarPriceAdapter(getApplicationContext(), getStrList02(), pos2));
                gridView02.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        TextView textView = (TextView) parent.getChildAt(position).findViewById(R.id.bt_content);
                        TextView textView1 = (TextView) parent.getChildAt(pos2).findViewById(R.id.bt_content);
                        textView1.setBackground(getDrawable(R.drawable.carprice_background));
                        textView1.setTextColor(getResources().getColor(R.color.carprice));
                        textView.setBackground(getDrawable(R.drawable.loginedittext_background));
                        textView.setTextColor(getResources().getColor(R.color.textcolor));
                        if (position == 0) {
                            Km = "";
                        } else {
                            Km = textView.getText().toString().split("万")[0];
                        }
                        count=1;
                        initData();
                        pos2 = position;
                    }
                });
                GridView gridView03 = (GridView) view2.findViewById(R.id.gv_price03);
                gridView03.setAdapter(new PopCarPriceAdapter(getApplicationContext(), getStrList03(), pos3));

                gridView03.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        TextView textView = (TextView) parent.getChildAt(position).findViewById(R.id.bt_content);
                        TextView textView1 = (TextView) parent.getChildAt(pos3).findViewById(R.id.bt_content);
                        textView1.setBackground(getDrawable(R.drawable.carprice_background));
                        textView1.setTextColor(getResources().getColor(R.color.carprice));
                        textView.setBackground(getDrawable(R.drawable.loginedittext_background));
                        textView.setTextColor(getResources().getColor(R.color.textcolor));
                        if (position == 0) {
                            carAge = "";
                        } else {
                            carAge = textView.getText().toString().split("年")[0];
                        }
                        count=1;
                        initData();
                        pos3 = position;
                    }
                });
                PopupWindow popWindow03 = new PopupWindow(view2, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
                popWindow03.setBackgroundDrawable(new ColorDrawable(0x000000));
                popWindow03.showAsDropDown(tv_01);
                break;
            case R.id.rl_defaut:
                invisiable();
                tv_defaut.setTextColor(getResources().getColor(R.color.discover));
                iv_defaut.setVisibility(View.VISIBLE);
                tv_01.setText(tv_defaut.getText());
                order = "0";
                count=1;
                initData();
                popWindow.dismiss();
                break;
            case R.id.rl_priceMin:
                invisiable();
                tv_priceMin.setTextColor(getResources().getColor(R.color.discover));
                iv_priceMin.setVisibility(View.VISIBLE);
                tv_01.setText(tv_priceMin.getText());
                order = "1";
                count=1;
                initData();
                popWindow.dismiss();
                break;
            case R.id.rl_priceMax:
                invisiable();
                tv_priceMax.setTextColor(getResources().getColor(R.color.discover));
                iv_priceMax.setVisibility(View.VISIBLE);
                tv_01.setText(tv_priceMax.getText());
                order = "2";
                count=1;
                initData();
                popWindow.dismiss();
                break;
            case R.id.rl_carAge:
                invisiable();
                tv_carAge.setTextColor(getResources().getColor(R.color.discover));
                iv_carAge.setVisibility(View.VISIBLE);
                tv_01.setText(tv_carAge.getText());
                order = "3";
                count=1;
                initData();
                popWindow.dismiss();
                break;
            case R.id.rl_kilo:
                invisiable();
                tv_kilo.setTextColor(getResources().getColor(R.color.discover));
                iv_kilo.setVisibility(View.VISIBLE);
                tv_01.setText(tv_kilo.getText());
                order = "4";
                count=1;
                initData();
                popWindow.dismiss();
                break;
        }
    }

    private void requestData() {
        list = new ArrayList<>();
        String url = MyUrl.Font_Url+MyUrl.queryTypes;
        RequestQueueUtils.exectueJsonRequest(this, url, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject o) {
                try {
                    JSONArray array = o.getJSONArray("data");
                    if("0".equals(o.getString("code"))){
                        for(int i=0;i<array.length();i++){
                           list.add(array.get(i).toString());
                        }

                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                ArrayAdapter arrayAdapter = new ArrayAdapter(getApplicationContext(),R.layout.car_brand_listitem,R.id.tv_carContent,list);
                lv_carBrand.setAdapter(arrayAdapter );
            }
        });
    }


    private void initView() {
        view = LayoutInflater.from(this).inflate(R.layout.pop_car_sort, null, false);
        view1 = LayoutInflater.from(this).inflate(R.layout.pop_car_brand, null, false);
        view2 = LayoutInflater.from(this).inflate(R.layout.pop_car_price, null, false);
        finish = (ImageButton) findViewById(R.id.finish);
        tv_01 = (TextView) findViewById(R.id.tv_01);
        tv_02 = (TextView) findViewById(R.id.tv_02);
        tv_03 = (TextView) findViewById(R.id.tv_03);
        lv_secondcar = (PullToRefreshListView) findViewById(R.id.lv_secondcar);

        finish.setOnClickListener(this);
        tv_01.setOnClickListener(this);
        tv_02.setOnClickListener(this);
        tv_03.setOnClickListener(this);
        rv_list = (RecyclerView) view1.findViewById(R.id.rv_list);
        rv_list.setAdapter(new PopCarBrandAdapter(SecondCarActivity.this, getArrayList()));
        manger = new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.HORIZONTAL);
        rv_list.setLayoutManager(manger);
        rl_defaut = (RelativeLayout) view.findViewById(R.id.rl_defaut);
        rl_defaut.setOnClickListener(this);
        rl_priceMin = (RelativeLayout) view.findViewById(R.id.rl_priceMin);
        rl_priceMin.setOnClickListener(this);
        rl_priceMax = (RelativeLayout) view.findViewById(R.id.rl_priceMax);
        rl_priceMax.setOnClickListener(this);
        rl_carAge = (RelativeLayout) view.findViewById(R.id.rl_carAge);
        rl_carAge.setOnClickListener(this);
        rl_kilo = (RelativeLayout) view.findViewById(R.id.rl_kilo);
        rl_kilo.setOnClickListener(this);
        tv_defaut = (TextView) view.findViewById(R.id.tv_defaut);
        tv_defaut.setOnClickListener(this);
        iv_defaut = (ImageView) view.findViewById(R.id.iv_defaut);
        tv_priceMin = (TextView) view.findViewById(R.id.tv_priceMin);
        iv_priceMin = (ImageView) view.findViewById(R.id.iv_priceMin);
        tv_priceMax = (TextView) view.findViewById(R.id.tv_priceMax);
        iv_priceMax = (ImageView) view.findViewById(R.id.iv_priceMax);
        tv_carAge = (TextView) view.findViewById(R.id.tv_carAge);
        iv_carAge = (ImageView) view.findViewById(R.id.iv_carAge);
        tv_kilo = (TextView) view.findViewById(R.id.tv_kilo);
        iv_kilo = (ImageView) view.findViewById(R.id.iv_kilo);

        lv_carBrand = (ListView)view1.findViewById(R.id.lv_carBrand);
    }

    private List<String> getStrList01() {
        List<String> strList = new ArrayList<>();
        strList.add("不限");
        strList.add("0-3万");
        strList.add("3-5万");
        strList.add("5-8万");
        strList.add("8-10万");
        strList.add("10-15万");
        strList.add("15-20万");
        strList.add("20-30万");
        strList.add("30-50万");
        strList.add("50-100万");
        strList.add("100万以上");
        return strList;
    }

    private List<String> getStrList02() {
        List<String> strList = new ArrayList<>();
        strList.add("不限");
        strList.add("1万以内");
        strList.add("3万以内");
        strList.add("6万以内");
        strList.add("10万以内");
        strList.add("20万以内");
        return strList;
    }

    private List<String> getStrList03() {
        List<String> strList = new ArrayList<>();
        strList.add("不限");
        strList.add("1年以内");
        strList.add("1-3年");
        strList.add("3-5年");
        strList.add("5-8年");
        strList.add("6-10年");
        strList.add("10年以上");
        return strList;
    }

    private ArrayList<PopCarBrandBean> getArrayList() {
        PopCarBrandBean popCarBrandBean = new PopCarBrandBean();
        PopCarBrandBean popCarBrandBean1 = new PopCarBrandBean();
        PopCarBrandBean popCarBrandBean2 = new PopCarBrandBean();
        PopCarBrandBean popCarBrandBean3 = new PopCarBrandBean();
        PopCarBrandBean popCarBrandBean4 = new PopCarBrandBean();
        PopCarBrandBean popCarBrandBean5 = new PopCarBrandBean();
        PopCarBrandBean popCarBrandBean6 = new PopCarBrandBean();
        PopCarBrandBean popCarBrandBean7 = new PopCarBrandBean();
        PopCarBrandBean popCarBrandBean8 = new PopCarBrandBean();
        PopCarBrandBean popCarBrandBean9 = new PopCarBrandBean();
        PopCarBrandBean popCarBrandBean10 = new PopCarBrandBean();
        popCarBrandBean.setCarBrand(R.mipmap.audi);
        popCarBrandBean.setCarName("奥迪");
        arrayList.add(popCarBrandBean);
        popCarBrandBean1.setCarBrand(R.mipmap.byd);
        popCarBrandBean1.setCarName("比亚迪");
        arrayList.add(popCarBrandBean1);
        popCarBrandBean2.setCarBrand(R.mipmap.dazhong);
        popCarBrandBean2.setCarName("大众");
        arrayList.add(popCarBrandBean2);
        popCarBrandBean3.setCarBrand(R.mipmap.ford);
        popCarBrandBean3.setCarName("福特");
        arrayList.add(popCarBrandBean3);
        popCarBrandBean4.setCarBrand(R.mipmap.kia);
        popCarBrandBean4.setCarName("起亚");
        arrayList.add(popCarBrandBean4);
        popCarBrandBean5.setCarBrand(R.mipmap.audi);
        popCarBrandBean5.setCarName("奥迪");
        arrayList.add(popCarBrandBean5);
        popCarBrandBean6.setCarBrand(R.mipmap.audi);
        popCarBrandBean6.setCarName("奥迪");
        arrayList.add(popCarBrandBean6);
        popCarBrandBean7.setCarBrand(R.mipmap.audi);
        popCarBrandBean7.setCarName("奥迪");
        arrayList.add(popCarBrandBean7);
        popCarBrandBean8.setCarBrand(R.mipmap.audi);
        popCarBrandBean8.setCarName("奥迪");
        arrayList.add(popCarBrandBean8);
        popCarBrandBean9.setCarBrand(R.mipmap.audi);
        popCarBrandBean9.setCarName("奥迪");
        arrayList.add(popCarBrandBean9);
        popCarBrandBean10.setCarBrand(R.mipmap.audi);
        popCarBrandBean10.setCarName("奥迪");
        arrayList.add(popCarBrandBean10);
        return arrayList;
    }

    @Override
    protected void onResume() {
        super.onResume();
        lv_secondcar.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(getApplicationContext(), SecondCarDetialActivity.class);
                intent.putExtra("id", addList.get(position-1).getId());
                startActivity(intent);
            }
        });
    }

    private void invisiable() {
        tv_defaut.setTextColor(getResources().getColor(R.color.secondcartext));
        iv_defaut.setVisibility(View.GONE);
        tv_priceMin.setTextColor(getResources().getColor(R.color.secondcartext));
        iv_priceMin.setVisibility(View.GONE);
        tv_priceMax.setTextColor(getResources().getColor(R.color.secondcartext));
        iv_priceMax.setVisibility(View.GONE);
        tv_carAge.setTextColor(getResources().getColor(R.color.secondcartext));
        iv_carAge.setVisibility(View.GONE);
        tv_kilo.setTextColor(getResources().getColor(R.color.secondcartext));
        iv_kilo.setVisibility(View.GONE);
    }
}
